package servlets.games;

import hibernate.method.GameMethods;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Servlet implementation class addGameLike
 */
@WebServlet("/addGameLike")
public class addGameLike extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public addGameLike() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String game = (String) request.getParameter("game_id");
		String user = (String) request.getParameter("user");
		
		
		int game_id = Integer.parseInt(game);
		int user_id = Integer.parseInt(user);
		
		
		PrintWriter out = response.getWriter();
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		GameMethods gm = new GameMethods();
		session.beginTransaction();
		
		gm.like(sessionFactory, session, user_id, game_id);
		
	
		
		try {
			session.getTransaction().commit();
			out.println("<script type=\"text/javascript\">alert(\"Polubiles gre!\"); window.location.href = \"social/index.jsp?page=games&group=liked\";</script>");
		} catch (Exception e) {
			out.println("<script type=\"text/javascript\">alert(\"Polubienie gry nie powiodlo sie\"); window.location.href = \"social/index.jsp?page=games&group=liked\";</script>");
		}
	}

}
